// @ts-nocheck
import * as cornerstone from "cornerstone-core"
import cornerstoneTools from "cornerstone-tools"
import * as dicomParser from "dicom-parser"
import cornerstoneMath from "cornerstone-math"
import Hammer from "hammerjs"
import cornerstoneWADOImageLoader from "cornerstone-wado-image-loader"
import cornerstoneWebImageLoader from 'cornerstone-web-image-loader'
import RectangleTool from './RectangleTool'
import MyRectangleRoiTool from "@/utils/MyRectangleRoi";
import MyFreehandRoi from "@/utils/MyFreehandRoi";
import { getOS } from "@/utils/window";

cornerstoneTools.external.cornerstone = cornerstone
cornerstoneTools.external.Hammer = Hammer
cornerstoneTools.external.cornerstoneMath = cornerstoneMath

cornerstoneTools.disableLogger()
cornerstoneTools.register('module', 'Rectangle', {configuration: {}, RectangleTool}, true)

cornerstoneTools.init({
  globalToolSyncEnabled: true,
  autoResizeViewports: true,
  showSVGCursors: false // attention: mouseButtonMask must include 1
})
// 指定要注册加载程序的基石实例
cornerstoneWebImageLoader.external.cornerstone = cornerstone
cornerstoneWADOImageLoader.external.cornerstone = cornerstone
cornerstoneWADOImageLoader.external.dicomParser = dicomParser
cornerstoneWADOImageLoader.external.cornerstoneMath = cornerstoneMath

const { isWin, systemInfo } = getOS()
cornerstoneWADOImageLoader.webWorkerManager.initialize({
  maxWebWorkers: (isWin && systemInfo !== 'Win10') ? 1 : navigator.hardwareConcurrency ? Math.max(navigator.hardwareConcurrency - 2, 1) : 1,
  startWebWorkersOnDemand: false,
  taskConfiguration: {
    decodeTask: {
      initializeCodecsOnStartup: false,
      usePDFJS: false,
      strict: false
    }
  }
})

// Set the tool font and font size
const fontFamily = "Work Sans, Roboto, OpenSans, HelveticaNeue-Light, Helvetica Neue Light, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif"
cornerstoneTools.textStyle.setFont(`16px ${fontFamily}`)
// Set the tool width
cornerstoneTools.toolStyle.setToolWidth(1)
// Set color for inactive tools
cornerstoneTools.toolColors.setToolColor("#fbb03b")
// Set color for active tools
cornerstoneTools.toolColors.setActiveColor("rgb(0, 255, 0)")
//快速翻层
cornerstoneTools.addTool(cornerstoneTools.StackScrollTool)
// cornerstoneTools.setToolActive('StackScroll', { mouseButtonMask: 1 });
// 鼠标滚轮事件
cornerstoneTools.addTool(cornerstoneTools.StackScrollMouseWheelTool)
cornerstoneTools.setToolActive('StackScrollMouseWheel', {})//默认开启
// 缩放
cornerstoneTools.addTool(cornerstoneTools.ZoomTool, {
  configuration: {
    invert: false,
    preventZoomOutsideImage: false,
    minScale: 0.05,
    maxScale: 20.0
  }
})

// 放大镜
// cornerstoneTools.addTool(cornerstoneTools.MagnifyTool)
// 移动
cornerstoneTools.addTool(cornerstoneTools.PanTool)
// 调节对比度
cornerstoneTools.addTool(cornerstoneTools.WwwcTool)
//十字线
// cornerstoneTools.addTool(cornerstoneTools.CrosshairsTool)
// cornerstoneTools.addTool(cornerstoneTools.ReferenceLinesTool)
// 旋转
// cornerstoneTools.addTool(cornerstoneTools.RotateTool)

const configuration = {
  handleRadius: 5,
  drawHandlesOnHover: true,
  hideHandlesIfMoving: false
}

//定制框(mpr)
cornerstoneTools.addTool(RectangleTool, {
  configuration: {
    ...configuration,
    // fillStyle: 'rgba(255,0,0,0.3)',
    // activeColor: 'rgba(255,255,0,0.5)',
  }
})
//定制框(超声视频)
cornerstoneTools.addTool(MyRectangleRoiTool, {
  configuration
})
//自由画笔
cornerstoneTools.addTool(MyFreehandRoi, {
  configuration:{
    // canComplete: true
  }
})
// 框
cornerstoneTools.addTool(cornerstoneTools.RectangleRoiTool, {
  configuration
})
// 角度
// cornerstoneTools.addTool(cornerstoneTools.AngleTool, {
//   configuration
// })
// 正圆
// cornerstoneTools.addTool(cornerstoneTools.CircleRoiTool, {
//   ...configuration,
//   hideHandlesIfMoving: true
// })
// 椭圆
// cornerstoneTools.addTool(cornerstoneTools.EllipticalRoiTool, {
//   ...configuration,
//   hideHandlesIfMoving: true
// })
//标尺
// cornerstoneTools.addTool(cornerstoneTools.ScaleOverlayTool, {
//   configuration: {
//     minorTickLength: 10,
//     majorTickLength: 15,
//     verticalLine: { start: { x: 0, y: 0 }, end: { x: 0, y: 0 } }
//   }
// })

// 默认开启
// cornerstoneTools.setToolActive('ScaleOverlay')
// cornerstoneTools.setToolActive('Overlay') //默认开启
// 开启覆盖模式
/*cornerstoneTools.addTool(cornerstoneTools.BaseOverLayerTool, {color:'#dacd73'});
cornerstoneTools.setToolActive("BaseOverLayer");*/

//预加载
cornerstoneTools.stackPrefetch.setConfiguration({
  preserveExistingPool: true,
  maxImagesToPrefetch: 1000 //Infinity
})
